package cst.start.teachresourceplatform118yfx.Fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.Toast;

import java.util.List;

import cst.start.teachresourceplatform118yfx.R;
import cst.start.teachresourceplatform118yfx.adapter.MyAdapter;
import cst.start.teachresourceplatform118yfx.bean.ArticleBean;
import cst.start.teachresourceplatform118yfx.iface.AmicoolListener;
import cst.start.teachresourceplatform118yfx.model.AmicoolModel;

public class TcaseFragmentSecond_1 extends Fragment {

    private View view;
    private Context context;
    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    MyAdapter myAdapter;
    AmicoolListener<List<ArticleBean>> listener;
    AmicoolModel resmodel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = getContext();
        view = inflater.inflate(R.layout.fragment,container,false);
        recyclerView = (RecyclerView)view.findViewById(R.id.recyclerview);
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        myAdapter = new MyAdapter(context,"tcase");
        recyclerView.setAdapter(myAdapter);

        SharedPreferences sp = context.getSharedPreferences("login",Context.MODE_PRIVATE);
        String sessionid = sp.getString("sessionid",null);
        System.out.println(sessionid);

        listener = new AmicoolListener<List<ArticleBean>>() {
            @Override
            public void onResponse(List<ArticleBean> l) {
                myAdapter.setList(l);
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        };

        resmodel = new AmicoolModel("gson");
        resmodel.getList("tcase",2,sessionid,listener);

        return view;
    }
}
